import store from '@/store'
import axios from 'axios'
const service = axios.create({
    baseURL: 'http://localhost:3000/api',
    timeout: 5000
})


service.interceptors.request.use(
    config => {
        if(store.state.user && store.state.user.token) {
            config.headers['Authorization'] = `Bearer ${store.state.user.token}`
        }
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        return response
    },
    error => {
        const errorMessage = error.response?.data || error.message
        console.log('请求失败',errorMessage)
        return Promise.reject(error)
    }
)

export default service